/**
 * @class NG.views.Index
 * @extends {Ext.Carousel}
 * 
 * First panel of the NetGis mobile application.<br>
 * All initial views and text drawn on this view.
 */
NG.views.Index = Ext.extend(Ext.Carousel, {
	layout:'fit',
	iconCls: 'info',
	dockedItems: [{
		xtype:'toolbar',
		title:'NetGis',
		ui:'light',
		items:[/*{
			text:'DenemeTusu',
			hidden: true,
			handler:function(){
				Ext.dispatch({
					controller: app.controllers.deneme,
					action: 'deneme',
					caller: app.views.index.getId(),
					animation: {type:'slide', direction:'right'}
				});
			}
		},*/
       {
			iconCls: 'bookmarks',
			iconMask: true,
			handler:function(){
				Ext.dispatch({
					controller: app.controllers.index,
					action: 'workspaces',
					animation: {type:'slide', direction:'right'}
				});					
			}
		}		       
		,{xtype: 'spacer'} //a space is added, buttons added after spacebar will be aligned to right
		,{
			text: L_NEW_WS,
            ui: 'confirm',               
			iconMask: true,			
			handler:function(){
				Ext.dispatch({
					controller: app.controllers.index,
					action: 'newWorkspace',
					caller: app.views.index.getId(),
					animation: {type:'slide', direction:'right'}
				});			
			}
		}]
	}]
	,items:[
	{
		html:'<p id="deviceProperties">Loading device properties...</p>' +
			'<div id="getmap-deneme">' +
			'<table width="100%"><tr><td align="center"><div id="login-deneme"></td></tr></table>'
		,listeners:{
			afterrender: function (component) {
		        var element = document.getElementById('deviceProperties');
		        element.innerHTML = app.getEnvironmentHTML();
				new Ext.Button({
					text: L_SETTINGS,
					iconCls: 'settings',
					iconMask: true,
					renderTo: 'login-deneme',
					width: 200,
					handler: function(){
						new NG.views.Settings().show();
					}
				});
				
				//Map view toolbar visibility toggle button
				new Ext.form.Toggle({
					renderTo: 'login-deneme',
					width: 200,
					value: 1, //Map view toolbar is displayed default.
					listeners: {
						change: function(slider, thumb, newValue, oldValue ) {
							if (app.views.viewport)
								Ext.dispatch({
									controller: app.controllers.index,
									action: 'toggleMapViewToolbar'
								});
						}
					}
				});
				
				
				new Ext.Button({
					text: 'Test',
					hidden: true,
					renderTo: 'login-deneme',
					width: 200,
					handler: function(){
						/*
						NGServiceRequester.login('netadmin', '1', URL_LOGIN2,{
							loginSuccess: function(sessionID){
								alert("Session ID: " + sessionID);
							}
						});*/
						
						var putInfoHatali = '<string>&lt;wfs:TransactionResponse xmlns:wfs="http://www.opengis.net/wfs/"&gt; &lt;wfs:TransactionResult&gt; &lt;Inserted&gt;' +
						'&lt;wfs:TransactionResult&gt; &lt;Message&gt;' +
						'Tabloda yapilmasini istediginiz degisiklikler, dizinde, birincil anahtarda veya iliskilerde yinelenen degerler olusturdukjlari için basarisiz oldu. Yinelenen verileri içeren alan veya alanlardaki verileri degistirin, dizini kaldirin veya dizini, yinelenen girdilere izin' +
						'verecek sekilde yeniden tanimlayin ve yeniden deneyin.' +
						'&lt;/Message&gt; &lt;!-- islemin neden gerçeklesmedigini bildiren hata metni burada gösterilir --&gt; &lt;wfs:Status&gt;' +
						'&lt;wfs:FAILED /&gt; &lt;!-- islemin basariyla gerçeklesip gerçeklesmedigi burada belirtilir --&gt; &lt;/wfs:Status&gt; &lt;/wfs:TransactionResult&gt; &lt;/Inserted&gt;' +
						'&lt;/wfs:TransactionResult&gt; &lt;/wfs:TransactionResponse&gt;</string>' ;
						
						var putInfoHatasiz = '<string>&lt;wfs:TransactionResponse xmlns:wfs="http://www.opengis.net/wfs/"&gt;'+
						  '&lt;wfs:TransactionResult&gt;' +
						    '&lt;Inserted&gt;0&lt;/Inserted&gt;' +
						    '&lt;Updated&gt;0&lt;/Updated&gt;' +
						   ' &lt;Deleted&gt;3&lt;/Deleted&gt;' +
						   ' &lt;wfs:Status&gt;' +
						   '   &lt;wfs:SUCCESS /&gt;' +
						  '  &lt;/wfs:Status&gt;' +
						 ' &lt;/wfs:TransactionResult&gt;' +
						'&lt;/wfs:TransactionResponse&gt;</string>';
						var pi = new NG.response.PutInfoEx({
							xmlData: putInfoHatali
						});
						alert(pi.isSuccessful());
						if (!pi.isSuccessful())
							alert(pi.getMessage());
					}
				});
				
				var picker = new Ext.form.ux.touch.GenericPickerField({
				//new Ext.form.Text({
				//	ui: 'select',
					hidden: true,
					renderTo: 'login-deneme',
					_key: null,
					width: 200,
					listeners: {
						
						focus1 : function ( t, e ) {
							var picker = new Ext.Picker({
								textField: t,
							    slots: [
							        {
							            name : 'limit_speed',
							            title: 'Speed',
							            data : [
							                {text: '50 KB/s', value: 50},
							                {text: '100 KB/s', value: 100},
							                {text: '200 KB/s', value: 200},
							                {text: '300 KB/s', value: 300}
							            ]
							        }
							    ],
							    listeners: {
							    	change : function ( p, obj) {
							    		console.log(obj.limit_speed);
							    		var arr =p.slots[0].data;
							    		for ( var i = 0; i < arr.length; i++) {
											var o = arr[i];
											if (o.value === obj.limit_speed) {
												p.textField.setValue(o.text);
												p.textField._key = o.value;
												break;
											}
										}
							    	} 
							    }
							});
							picker.show();							
						} 
						
					}
				});				
				
				new Ext.Button({
					text: '...',
					hidden: true,
					renderTo: 'login-deneme',
					width: 200,
					handler: function(){
						picker.setData([{text: 'xx', value: 1},{text: 'yy', value: 2}]);
						/*
						var session = NGServiceRequester.isAlreadyLoggedIn('netadmin',URL_LOGIN2);
						if (Ext.isEmpty(session)) {alert('login olun'); return;}
							
						var delegate = {
							requestFinished: function (response) {
		                		alert("Finish NetworkInfo: " + response);
				        		var networkInfoResponse = new NG.response.NetworkInfo({
				        			xmlData: response
				        		});
		                	},
		                	requestFail: function (response) {
		                		alert("Fail(NetworkInfo): " + response.status);
		                	}
		                };
		                var ni = new NG.command.NetworkInfo({
		                	workspaceName: "PENDIK_UTM",
		                	runXmlUrl: URL_RUNXML2
		                });
						NGServiceRequester.executeCommand(ni, session.sessionID, delegate);
						*/
					}
				});
				
				new Ext.Button({
					hidden:true,
					text: 'GetMap Deneme',
					renderTo: 'getmap-deneme',
					width: 200,
					handler: function(){
						function getMap(sessionID){
			                var delegate = {
			                	requestFinished: function (response) {
			                		alert("Finish GetMap: " + response);
					        		var getMapResponse = new NG.response.GetMap({
					        			xmlData: response
					        		});
					        		alert("GetMap url: " + getMapResponse.getMapUrl());
			                	},
			                	requestFail: function (response) {
			                		alert("Fail(GetMap): " + response.status);
			                	}
			                };
			                var gm = new NG.command.GetMap({
			                	workspaceName: "PENDIK_UTM"                	
			                });
							NGServiceRequester.executeCommand(gm, delegate, sessionID);													
						}
						
						var sessionID = "";
						var session = NGServiceRequester.isAlreadyLoggedIn('netadmin',URL_LOGING);
						if (session) {
							sessionID = session.sessionID;
							getMap(sessionID);
						} else {
							NGServiceRequester.login('netadmin', '1', URL_LOGIN2);
							NGServiceRequester.on('loginSuccess', function(response, opts, sessionID){
								getMap(sessionID);
							});							
							NGServiceRequester.on('loginFail', function(response, opts){
								alert("Login failed to " + URL_LOGIN2);
							});
						}
					}
				});				
			} 
		}		
	},{
		id: 'netgis-log-panel',
	    bodyPadding: 10,
	    defaults: {
	    },
		items: [
			new Ext.form.TextArea({
				id: 'netgis-log-area'
			}),
			new Ext.Button({
				text: 'Temizle',
				width: 200,
				handler: function (){
					Ext.getCmp('netgis-log-area').setValue('');
				}
			})
		]
	}]
	,initComponent: function() {
		NG.views.Index.superclass.initComponent.apply(this, arguments);
	}
});
